package com.cqupt.controller;

import com.cqupt.common.log.annotation.OperationLog;
import com.cqupt.pojo.ResponseMessage;
import com.cqupt.service.HostMapService;
import com.cqupt.service.impl.HostMapServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.awt.print.Printable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @auther axb
 * @Description 注意：关于机房机柜的操作目前并未涉及，保留了接口
 * @date 2021/3/17 - 20:22
 */
@RestController
@CrossOrigin
@RequestMapping("map")
public class HostMapController {
    @Autowired
    private HostMapService mapService;

    /**
     * 获取若有拓扑图名称及其对应的sysmapid
     * 请求方式-get
     * @return
     */
    @RequestMapping("getNamesOfMap")
    public ResponseMessage getNamesOfMap() {
        ResponseMessage responseMessage = new ResponseMessage();

        // 数据获取
        List<Map<String, Object>> result = mapService.getNamesOfMap();
        // 封装返回结果
        Map<String, Object> resultMap = new HashMap<String, Object>(){{
            put("result", result);
        }};
//        状态设置
        responseMessage.setStatus(true);
        responseMessage.setContent(resultMap);
        return responseMessage;
    }

    /**
     * 根据拓扑图名称获取拓扑图详细内容
     * 请求方式-get
     * @param name 拓扑图名称(String)-Not Null
     * @return
     */
    @RequestMapping("getMapByName")
    public ResponseMessage getNamesOfMap(final String name) {
        ResponseMessage responseMessage = new ResponseMessage();

        // 数据获取
        List<Map<String, Object>> result = mapService.getMapByName(name);
//        封装返回结果
        Map<String, Object> resultMap = new HashMap<String, Object>(){{
            put("result", result);
        }};
//        状态设置
        responseMessage.setStatus(true);
        responseMessage.setContent(resultMap);
        return responseMessage;
    }

    /**
     * 根据拓扑图id获取拓扑图中的内容
     * 请求方式-get
     * @param sysmapids 拓扑图id（String）-Not Null
     * @return
     */
    @RequestMapping("getMapBySysmapids")
    public ResponseMessage getMapBySysmapid(final String sysmapids) {
        ResponseMessage responseMessage = new ResponseMessage();

        // 数据请求
        List<Map<String, Object>> result = mapService.getMapBySysmapid(sysmapids);
//        封装返回结果
        Map<String, Object> resultMap = new HashMap<String, Object>(){{
            put("result", result);
        }};
//        状态设置
        responseMessage.setStatus(true);
        responseMessage.setContent(resultMap);
        return responseMessage;
    }

    /**
     * 获得所有拓扑图的详细信息
     * 请求方式-get
     * @return Map<String, Object>
     */
    @RequestMapping("getAllMap")
    public ResponseMessage getAllMap() {
        ResponseMessage responseMessage = new ResponseMessage();

        // 数据获取
        List<Map<String, Object>> result = mapService.getAllMap();
//        封装返回结果
        Map<String, Object> resultMap = new HashMap<String, Object>(){{
            put("result", result);
        }};
//        状态设置
        responseMessage.setStatus(true);
        responseMessage.setContent(resultMap);
        return responseMessage;
    }

    /**
     * 更新拓扑图
     * 请求方式-post
     * @param params 拓扑图更新信息（Map<String,Object>）-Not Null
     * @return
     */
    @OperationLog(level = "信息", object = "拓扑", operationType = "修改")
    @RequestMapping("updateMap")
    public ResponseMessage updateMap(@RequestBody Map<String,Object> params) {
        ResponseMessage responseMessage = new ResponseMessage();
        // 更新
        Map<String, Object> resultMap = mapService.updateMap(params);

        // 结果封装并返回
        Map<String, Object> result = new HashMap<>();
        result.put("log_objectName",params.get("name"));
//        更新失败
        if(resultMap.get("error") != null) {
            responseMessage.setStatus(false);
            responseMessage.setErrorCode(resultMap.get("error").toString());
//        更新成功
        } else {
            responseMessage.setStatus(true);
            result.put("result", resultMap.get("result"));
            result.put("log_description", "");
        }
        responseMessage.setContent(result);
        return responseMessage;
    }

    /**
     * 创建拓扑图
     * 请求方式-post
     * @param params 参数见HostMapServiceImpl-handleTopoParams()
     * @return "sysmapids"
     */
    @OperationLog(level = "信息", object = "拓扑", operationType = "新增")
    @RequestMapping("createMap")
    public ResponseMessage createMap(@RequestBody Map<String,Object> params) {
        ResponseMessage responseMessage = new ResponseMessage();

        // 请求发送
        Map<String, Object> resultMap = mapService.createMap(mapService.handleTopoParams(params));

        // 结果封装并添加日志信息
        Map<String, Object> resMap = new HashMap<>();
//        创建出错
        if(resultMap.get("error") != null) {
            responseMessage.setStatus(false);
            responseMessage.setErrorCode(resultMap.get("error").toString());
            resMap.put("log_objectName", params.get("name").toString());
//        创建成功
        }else {
            responseMessage.setStatus(true);
            resMap = (Map<String, Object>) resultMap.get("result");
            resMap.put("log_objectName", params.get("name").toString());
            resMap.put("log_description","");
        }
        responseMessage.setContent(resMap);
        return responseMessage;
    }

    /**
     * 删除拓扑图
     * 请求方式-get
     * @param sysmapid 拓扑图id（String）-Not Null
     * @return
     */
    @OperationLog(level = "信息", object = "拓扑", operationType = "删除")
    @RequestMapping("deleteMap")
    public ResponseMessage deleteMap(String sysmapid) {
        ResponseMessage responseMessage = new ResponseMessage();

        // 删除执行
        Map<String, Object> resultMap = mapService.deleteMapById(sysmapid);

        // 结果封装并添加日志信息
        resultMap.put("log_objectName",sysmapid);
//        删除出错
        if(resultMap.get("error") != null) {
            responseMessage.setStatus(false);
            responseMessage.setErrorCode(resultMap.get("error").toString());
//        删除成功
        } else {
            responseMessage.setStatus(true);
            resultMap.put("log_description","");
        }

        responseMessage.setContent(resultMap);
        return responseMessage;
    }

    /**
     * 查询所有机房或单个机房-get请求（暂不支持单个查询）-无参
     * @return
     */
    @RequestMapping("getAllEngineRoomsOrsingle")
    public ResponseMessage getAllEngineRoomsOrsingle(){
        ResponseMessage responseMessage = new ResponseMessage();
        List<Map<String, Object>> resultList = mapService.getEngineRoom(null);

        Map<String, Object> result = new HashMap<>();
        result.put("result",resultList);

        responseMessage.setStatus(true);
        responseMessage.setContent(result);
        return responseMessage;
    }

    /**
     * 创建机房 post请求
     * @param params name(String-不为空 且 全局唯一)、
     *               description（String-选填）、
     *               engineroom_number(String-不为空 且 全局唯一)、
     * @return
     */
    @RequestMapping("createEngineRoom")
    public ResponseMessage createEngineRoom(@RequestBody Map<String,Object> params){
        ResponseMessage responseMessage = new ResponseMessage();
        Map<String, Object> res = new HashMap<>();
        // 数据判空
        int result = 0;
        if(params.get("name")!=null & !"".equals(params.get("name")) & params.get("engineroom_number")!=null & !"".equals(params.get("engineroom_number"))){
            // 进行插入
            result = mapService.createEngineRoom(params);
            if(result != 1){
                responseMessage.setStatus(false);
                res.put("error","插入失败");
            }else{
                responseMessage.setStatus(true);
                res.put("result","插入成功");
            }
        }else {
            responseMessage.setStatus(false);
            res.put("error","插入失败，请核对参数");
        }
        responseMessage.setContent(res);
        return responseMessage;
    }

    /**
     * 根据engineroom_number更新机房相关信息（engineroom_number不提供更新）post请求
     * @param params name(String-不为空 且 全局唯一)、
     *               description（String-选填）、
     *               engineroom_number(String-不为空 且 全局唯一)、
     * @return
     */
    @RequestMapping("updateEngineInformation")
    public ResponseMessage updateEngineInformation(@RequestBody Map<String,Object> params){
        ResponseMessage responseMessage = new ResponseMessage();
        Map<String, Object> res = new HashMap<>();
        // 数据判空
        int result = 0;
        if(params.get("name")!=null & !"".equals(params.get("name")) & params.get("engineroom_number")!=null & !"".equals(params.get("engineroom_number"))){
            // 进行更新
            result = mapService.updateEngineRoom(params);
            if(result != 1){
                responseMessage.setStatus(false);
                res.put("error","更新失败");
            }else{
                responseMessage.setStatus(true);
                res.put("result","更新成功");
            }
        }else {
            responseMessage.setStatus(false);
            res.put("error","更新失败，请核对参数");
        }
        responseMessage.setContent(res);
        return responseMessage;
    }

    /**
     * 批量或删除单个机房-post请求
     * @param params engineRoomNumber(Array)
     * @return
     */
    @RequestMapping("deleteEngineRoomSingleOrMore")
    public ResponseMessage deleteEngineRoomSingleOrMore(@RequestBody Map<String,Object> params){
        ResponseMessage responseMessage = new ResponseMessage();
        Map<String, Object> res = new HashMap<>();

        // 参数不为空的情况下执行请求
        if(params!=null){
            ArrayList<String> numberList = (ArrayList<String>) params.get("engineRoomNumber");
            if(numberList != null){
                int flag = numberList.size();
                if(flag == mapService.deleteEngineRoom(numberList)){
                    responseMessage.setStatus(true);
                    res.put("result","删除成功");
                }
            }
        }else{
            responseMessage.setStatus(false);
            res.put("error","删除失败");
        }
        responseMessage.setContent(res);
        return responseMessage;
    }

    /**
     * 创建机柜-post请求
     * @param params 名称-name(String-不为空且全局唯一)
     *               编号-serial_number(String-不为空且全局唯一)
     *               所属机房编号(外键)-engineroom_number(String-不为空)
     *               描述-description(String)
     *               关联拓扑图编号-sysmapids(String)
     *               关联拓扑图名称-map_name(String)
     * @return
     */
    @RequestMapping("buildCabinet")
    public ResponseMessage buildCabinet(@RequestBody Map<String,Object> params){
        ResponseMessage responseMessage = new ResponseMessage();
        Map<String, Object> res = new HashMap<>();
        // 数据判空
        if(params.get("name")!=null & !"".equals(params.get("name")) &
           params.get("serial_number")!=null & !"".equals(params.get("serial_number")) &
           params.get("engineroom_number")!=null & !"".equals(params.get("engineroom_number"))){
            if(mapService.createCabinet(params) != 1){
                responseMessage.setStatus(false);
                res.put("error","插入失败");
            }else{
                responseMessage.setStatus(true);
                res.put("result","插入成功");
            }
        }else {
            responseMessage.setStatus(false);
            res.put("error","插入失败，请核对参数");
        }
        responseMessage.setContent(res);
        return responseMessage;
    }

    /**
     * 根据机房编号查询其下所有机柜，或根据机柜编号查询（暂未提供）-post请求
     * @param params 机房编号-engineroom_number(String)
     * @return
     */
    @RequestMapping("getAllCabinetBelongToRoom")
    public ResponseMessage getAllCabinetBelongToRoom(@RequestBody Map<String,Object> params){
        ResponseMessage responseMessage = new ResponseMessage();
        Map<String, Object> result = new HashMap<>();

        if(!"".equals(params.get("engineroom_number"))){
            List<Map<String, Object>> resultList = mapService.getCabinet(params);
            result.put("result",resultList);
            responseMessage.setStatus(true);
            responseMessage.setContent(result);
        }else{
            result.put("error","参数不能为空");
            responseMessage.setStatus(false);
            responseMessage.setContent(result);
        }
        return responseMessage;
    }

    /**
     * 批量或删除单个机柜-post请求
     * @param params serial_numbers(Array)
     * @return
     */
    @RequestMapping("deleteCabinetSingleOrMore")
    public ResponseMessage deleteCabinetSingleOrMore(@RequestBody Map<String,Object> params){
        ResponseMessage responseMessage = new ResponseMessage();
        Map<String, Object> res = new HashMap<>();

        if(params!=null){
            ArrayList<String> numberList = (ArrayList<String>) params.get("serial_numbers");
            if(numberList != null){
                int flag = numberList.size();
                if(flag == mapService.deleteCabinet(numberList)){
                    responseMessage.setStatus(true);
                    res.put("result","删除成功");
                }
            }
        }else{
            responseMessage.setStatus(false);
            res.put("error","删除失败");
        }
        responseMessage.setContent(res);
        return responseMessage;
    }
}
